package com.chain.BrotherExchange.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;

import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.BaseResponseModel;
import com.chain.BrotherExchange.data.model.ExtractCoinAddressList;
import com.chain.BrotherExchange.utils.Androids;

import java.util.List;

import static com.chain.BrotherExchange.viewmodel.VerificationManage.verifyAddress;
import static com.chain.BrotherExchange.viewmodel.VerificationManage.verifyExtractCoinType;
import static com.chain.BrotherExchange.viewmodel.VerificationManage.verifyRemark;

/**
 * (提币)地址管理
 */
public class ExtractCoinAddressViewModel extends AndroidViewModel {

    private final int uid = Androids.LocalConfig.getUid();
    private final String token = Androids.LocalConfig.getToken();

    private MutableLiveData<Resource<BaseResponseModel>> mSingleLiveData;
    private MutableLiveData<Resource<List<ExtractCoinAddressList.DataBeanX.DataBean>>>
            mAddressListLiveData;

    public ExtractCoinAddressViewModel(@NonNull Application application) {
        super(application);

        mSingleLiveData = new MutableLiveData<>();
        mAddressListLiveData = new MutableLiveData<>();
    }

    /**
     * 提币地址列表
     */
    public MutableLiveData<Resource<List<ExtractCoinAddressList.DataBeanX.DataBean>>> addressList() {
        mAddressListLiveData.setValue(Resource.loading());
        DataRepository.get().extractAddressList(token, uid, result -> {
            ExtractCoinAddressList.DataBeanX resultData = result.getData();
            if (result.isSucceed() && resultData != null) {
                List<ExtractCoinAddressList.DataBeanX.DataBean> list = resultData.getDataX();
                if (list != null && !list.isEmpty()) {
                    mAddressListLiveData.setValue(Resource.success(list));
                } else {
                    mAddressListLiveData.setValue(Resource.empty());
                }

            } else if (result.isNotNetwork()) {
                mAddressListLiveData.setValue(Resource.notNetwork());

            } else {
                mAddressListLiveData.setValue(Resource.error(result.getMsg()));
            }
        });

        return mAddressListLiveData;
    }

    /**
     * 添加提币地址
     */
    public MutableLiveData<Resource<BaseResponseModel>> addAddress(int id, String address, String remark) {
        if (verifyExtractCoinType(id) && verifyAddress(address) && verifyRemark(remark)) {
            mSingleLiveData.setValue(Resource.loading());
            DataRepository.get().addCoinAddress(token, uid, id, address, remark, result -> {
                if (result.isSucceed()) {
                    mSingleLiveData.setValue(Resource.success(result, result.getMsg()));
                } else {
                    mSingleLiveData.setValue(Resource.error(result.getMsg()));
                }
            });
        }

        return mSingleLiveData;
    }

    /**
     * 删除提币地址
     */
    public MutableLiveData<Resource<BaseResponseModel>> deleteAddress(int id) {
        String addressId = String.valueOf(id);
        DataRepository.get().deleteExtractAddress(token, uid, addressId, result -> {
            if (result.isSucceed()) {
                mSingleLiveData.setValue(Resource.success(result, result.getMsg()));
            } else {
                mSingleLiveData.setValue(Resource.error(result.getMsg()));
            }
        });

        return mSingleLiveData;
    }
}
